package com.wt.struggle.goodbye;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.multidex.MultiDex;
import android.util.Log;
import com.danikula.videocache.HttpProxyCacheServer;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.wt.struggle.goodbye.base.Share;
import com.wt.struggle.goodbye.wxutil.Contact;

import java.util.ArrayList;
import java.util.List;

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        boolean isTui = Share.getTui(getApplicationContext());

        if (isTui) {

            Log.i("result", "执行友盟推送");

            UMConfigure.init(this, Contact.UM_KEY, "Umeng", UMConfigure.DEVICE_TYPE_PHONE, Contact.UM_APPSECRET);
            // 获取消息推送代理示例
            PushAgent mPushAgent = PushAgent.getInstance(this);
            // 注册推送服务，每次调用register方法都会回调该接口
            mPushAgent.register(new IUmengRegisterCallback() {

                @Override
                public void onSuccess(String deviceToken) {

                    Log.i("result", "友盟注册返回----" + deviceToken);
                    Share.saveDeviceToken(getApplicationContext(), deviceToken);

                }

                @Override
                public void onFailure(String s, String s1) {

                }
            });

            mPushAgent.setNotificationClickHandler(notificationClickHandler);
            mPushAgent.setDisplayNotificationNumber(5);

///            mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER); //服务端控制声音

            mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);//客户端允许呼吸灯点亮
            mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);
        }
    }

    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(base);
    }

    UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {

        @Override
        public void dealWithCustomAction(Context context, UMessage msg) {

            Log.i("result","执行打开数据------1111111");
            if (isRunningInForeground()) {

                Log.i("result","执行打开数据------");
                Intent intent = new Intent(context, MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.putExtra("code", 2);
                startActivity(intent);
            } else {
                reOpenApp();
            }

        }
    };


    private void reOpenApp() {
        PackageManager packageManager = getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(getPackageName());

        App.exit();

        if (intent != null) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
        }

    }


    public static List<Activity> list = new ArrayList<>();

    public static void addActivity(Activity activity) {
        list.add(activity);

    }

    public static void exit() {
        for (Activity temp : list) {
            temp.finish();
        }

    }

    private HttpProxyCacheServer proxy;

    public static HttpProxyCacheServer getProxy(Context context) {
        App app = (App) context.getApplicationContext();
        return app.proxy == null ? (app.proxy = app.newProxy()) : app.proxy;
    }

    private HttpProxyCacheServer newProxy() {
        return new HttpProxyCacheServer(this);
    }


    /**
     * 关闭指定界面
     */
    public static void finishIndexActivity() {


    }


    /**
     * 判断程序是否打开
     */
    public boolean isRunningInForeground() {

        boolean isActivityFound = false;

        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> services = activityManager
                .getRunningTasks(1);

        if (services.get(0).topActivity.getPackageName().equalsIgnoreCase(getApplicationContext().getPackageName())) {
            isActivityFound = true;
        }
        return isActivityFound;
    }

}
